package com.springboot.wisdom.email.service.impl;

import com.springboot.wisdom.common.common.RedisCache;
import com.springboot.wisdom.common.common.ResultUtil;
import com.springboot.wisdom.common.common.bean.MyResult;
import com.springboot.wisdom.common.common.constant.LogStatusEnum;
import com.springboot.wisdom.common.util.EmailUtil;
import com.springboot.wisdom.common.util.SystemOSUtil;
import com.springboot.wisdom.common.util.UserUtils;
import com.springboot.wisdom.email.service.EmailService;
import com.springboot.wisdom.log.domain.vo.LogVO;
import com.springboot.wisdom.log.service.LogService;
import com.springboot.wisdom.serviceApi.email.model.vo.EmailVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * 综合逻辑业务。
 */
@Service
@Slf4j
public class EmailServiceImpl implements EmailService {

    /**
     * 发送邮箱的主题名称。
     */
    @Value("${project.name}")
    private String projectName;

    @Resource
    private LogService logService;

    @Resource
    private RedisCache redisCache;

    @Resource
    private EmailUtil emailUtil;

    @Resource
    private UserUtils userUtils;


    @Override
    public MyResult<String> sendAttachmentsMail(EmailVO emailVO) {

        // 上传文件。【去除文件前缀】
        String fileWebUrl = SystemOSUtil.getFileWebUrl();
        String filePath = SystemOSUtil.getFilePath();
//        System.out.println("fileWebUrl = " + fileWebUrl);
//        System.out.println("filePath = " + filePath);
        String[] newFilePaths = new String[emailVO.getFilePaths().length];

        String[] filePaths = emailVO.getFilePaths();
        for (int i = 0; i < filePaths.length; i++) {
//            System.out.println("filePaths[i].split(fileWebUrl)[1] = " + filePaths[i].split(fileWebUrl)[1]);
            newFilePaths[i] = filePath + filePaths[i].split(fileWebUrl)[1];
        }
        emailVO.setFilePaths(newFilePaths);
//        System.out.println("emailVO.getFilePaths() = " + Arrays.toString(emailVO.getFilePaths()));

        // 发送邮箱，
        Boolean sendAttachmentsMailStatus = emailUtil.sendAttachmentsMail(
                emailVO.getToEmail(), emailVO.getSubject(), emailVO.getContent()
                , emailVO.getFilePaths(), emailVO.getFileNames()
        );
        if (sendAttachmentsMailStatus) {
            logService.addInfo(new LogVO(LogStatusEnum.SEND_EMAIL, "邮件发送给 " + emailVO.getToEmail() + " 成功！", ""));
        }
        return sendAttachmentsMailStatus
                ? ResultUtil.success("邮箱发送成功！") : ResultUtil.error("邮箱发送失败！请检查邮箱是否正确！");
    }


    /**
     * 获取邮箱验证码，获取新验证码。
     *
     * @param email
     * @return
     */
    @Override
    public MyResult<String> getEmailCode(String email) {
        String verificationCode = getVerificationCode();

        if (emailUtil.sendHtmlEmail(email, "【" + projectName + "】验证码", "验证码：【" + verificationCode + "】，有效时间：5分钟！")) {
            redisCache.setCacheObject(email, verificationCode, 5, TimeUnit.MINUTES);
            return ResultUtil.success("获取验证码成功！");
        }
        return ResultUtil.error("获取验证码失败");
    }

    @Override
    public MyResult<Boolean> checkEmail(String email, String code) {
//        String emailCode = (String) redisCache.getCacheObject(email);
//        System.out.println("isEmailCode(email) = " + isEmailCode(email));
        return isEmailCode(email) ?
                ResultUtil.success(true) :
                ResultUtil.error("邮箱验证码有误！");
    }


    /**
     * 校验邮箱是否有验证码。
     *
     * @param email
     * @return
     */
    @Override
    public boolean isEmailCode(String email) {
//        System.out.println("StringUtils.hasLength(selectEmailCode(email)) = " + StringUtils.hasLength(selectEmailCode(email)));
        return StringUtils.hasLength(selectEmailCode(email));
    }

    /**
     * 查询邮箱验证码。【不创建新验证码】
     * 查询为 null，表示验证码错误/验证码过期。
     *
     * @param email
     * @return 正确的验证码。
     */
    @Override
    public String selectEmailCode(String email) {
        log.info("邮箱验证码为：{}", redisCache.getCacheObject(email) + "");
        return redisCache.getCacheObject(email);
    }


    /**
     * 获取验证码。
     *
     * @return
     */
    private String getVerificationCode() {
        //1，用随机生成数方法，生成验证码
        Random random = new Random();                          //定义一个随机生成数技术，用来生成随机数
        StringBuilder randomData = new StringBuilder();                                   //定义一个空的Atring变量用来接收生成的验证码
        for (int i = 0; i < 6; i++) {                       //循环6次每次生成一位，6位验证码
            int a = random.nextInt(3);                             //验证码包括数字、大小写字母组成
            switch (a) {                                          //a:    0       1       2
                case 0:                                         //      数字   小写字母   大写字母
                    char s = (char) (random.nextInt(26) + 65);
                    randomData.append(s);
                    break;
                case 1:
                    char s1 = (char) (random.nextInt(26) + 97);
                    randomData.append(s1);
                    break;
                case 2:
                    int s2 = random.nextInt(10);
                    randomData.append(s2);
                    break;
            }
        }
        System.out.println("用随机生成数方法，生成的验证码:" + randomData);
        return randomData.toString();
    }
}